package com.bdqn.tour.filter;

import com.bdqn.tour.pojo.City;
import com.bdqn.tour.pojo.PageInfo;
import com.bdqn.tour.pojo.Scenic;
import com.bdqn.tour.pojo.User;

import com.bdqn.tour.service.CityService;
import com.bdqn.tour.service.GyhQueryALLUserInfoService;
import com.bdqn.tour.service.HqfSearchMessageService;
import com.bdqn.tour.service.impl.CityServiceimpl;
import com.bdqn.tour.service.impl.GyhQueryALLUserInfoServiceImpl;
import com.bdqn.tour.service.impl.hqfSearchMessageServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * packageName com.csdj.web.filter
 *
 * @author winver
 * @version 1.0
 * @className CharacterFilter
 * @date 2024/11/28
 * @description TODO
 */

@WebFilter(urlPatterns = "/*")
public class CharacterFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        //获取session
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 获取 session
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
        List<String> executeFilter = Arrays.asList(".js",".png",".jpg",".css",".gif");
        boolean isSuc = false;
        for (String suf : executeFilter) {
            if(uri.endsWith(suf)){
                isSuc = true;
                break;
            }
        }

        if(!isSuc){
            String pageNoStr = request.getParameter("pageNo");
            String pageSizeStr = request.getParameter("pageSize");
            int pageNum = 1;
            int pageSize = 5;
            if(pageNoStr != null && !"".equals(pageNoStr)){
                pageNum = Integer.parseInt(pageNoStr);
            }
            if(pageSizeStr != null && !"".equals(pageSizeStr)){
                pageSize = Integer.parseInt(pageSizeStr);
            }



            //调用业务层查询全部用户信息
            GyhQueryALLUserInfoService service = new GyhQueryALLUserInfoServiceImpl();

            PageInfo<User> pageInfo = service.getPageEmp(pageNum, pageSize);
            //存入session集合
            session.setAttribute("pageInfo",pageInfo);

            //酒店业务
            //调用业务层
            CityService cityService = new CityServiceimpl();
            //获取分页数据
            List<City> show = cityService.show(pageNum, pageSize);
            int count = cityService.count();
            PageInfo<City> jdshow = new PageInfo<>();
            jdshow.setCurrPageNo(pageNum);
            jdshow.setPageSize(pageSize);
            jdshow.setList(show);
            jdshow.setTotals(count);
            //存储分页数据
            session.setAttribute("jdshow", jdshow);

             //景点业务
            //调用业务层查询全部用户信息
            HqfSearchMessageService searchMessageService = new hqfSearchMessageServiceImpl();
            PageInfo<Scenic> hqfpageInfo = searchMessageService.getPageEmp(pageNum, pageSize);
            //存入session集合
            session.setAttribute("hqfPageInfo",hqfpageInfo);
        }
      /*  //调用业务层
        GyhQueryALLUserInfoService service  = new GyhQueryALLUserInfoServiceImpl();
        //从数据库中获取数据 查询
        List<User> userList = service.getQueryALLUserInfo();
        //将数据储存到session
        session.setAttribute("userList",userList);*/
        //分页参数
        filterChain.doFilter(request,servletResponse);//放行 继续下一步
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
